! function($, survey) {
	var _currentInterAddress = '';

	function _options(showLoading, callback) {
		var min = $('meta[name="viewport"]').size() > 1 ? false : true;
		return {
			type: 'POST',
			url: '',
			data: {},
			contentType: 'application/json; charset=utf-8',
			// 默认是application/x-www-form-urlencoded
			dataType: 'JSON',
			timeout: 60000,
			async: true,
			cache: false,
			beforeSend: function(xhr, settings) {
				if (showLoading && window.loading) {
					loading.open(min);
				}
			},
			success: function(data, status, xhr) {
				survey.log('***(' + _currentInterAddress + ')ajax成功返回数据：' + data.substring(0, 500));
				var res = {};
				try {
					var d = JSON.parse(data);
					res.status = '000000';
					res.responseBody = d;
					//如果返回状态为-1，则是登录超时或者未登录，直接跳转到登录页面
					if (Number(d.code) === -1) {
						survey.redirection('../pages/ajax.ftl');
						return;
					}
				} catch (e) {
					res.status = '000003';
					res.responseBody = '';
					res.errorMsg = '数据格式错误，请重试';
					survey.log('***(' + _currentInterAddress + ')ajax成功返回数据，但是转JSON错误：' + e);
				}
				$.isFunction(callback) && callback(res);
			},
			error: function(xhr, errorType, error) {
				survey.log('***(' + _currentInterAddress + ')ajax失败返回数据：errorType=' + errorType + '  xhr=' + JSON.stringify(xhr) +
					' error=' + error);
				var errorMsg = '请求失败，请重试！';
				if (errorType === "abort") {
					//无网络
					errorMsg = "网络已断开";
				} else if (errorType === "timeout") {
					//超时
					errorMsg = "系统连接超时";
				} else if (errorType === "error") {
					//服务器或者客户端错误
					switch (xhr.status) {
						case 303:
							errorMsg = '重定向';
							survey.redirection('../pages/303.html');
							break;
						case 404:
							errorMsg = "未找到服务器";
							survey.redirection('../pages/404.html');
							break;
						case 500:
							//内部服务器错误。
							errorMsg = "服务器未响应";
							break;
						case 503:
							//服务不可用。这个错误代码为IIS6.0所专用。
							errorMsg = "服务器不可用";
							break;
						case 504:
							//网关超时。
							errorMsg = "网关超时";
							break;
					}
				}
				if ($.isFunction(callback)) {
					callback({
						status: '000001',
						errorMsg: errorMsg
					});
				}
			},
			complete: function(xhr, status) {
				if (showLoading && window.loading) {
					var time = window.setInterval(function() {
						var loadingExist = loading.isloaded();
						if (loadingExist) {
							window.clearInterval(time);
							loading.close();
						}
					}, 500);
				}
			}
		};
	}

	function _ajax(params, callback) {
		//是否加载loading框
		var showLoading = typeof params.showLoading === 'boolean' ? params.showLoading : true;
		//ajax请求地址
		var requestURL = survey.server() + params.url || '';
		//请求参数
		var requestData = params.request || {};
		//统一增加一个来源2018-04-02
		requestData.dataSource = 'h5_report';

		console.log(requestData)
		var requestDataFilter = [];
		for (var key in requestData) {
			requestDataFilter.push(requestData[key])
		}
		console.log(requestDataFilter.join(''));
		survey.verifySpecialCharts(requestDataFilter.join(''))
		if (survey.verifySpecialCharts(requestDataFilter.join(''))) {
			survey.prompt("不能包含特殊字符");
			return;
		}
		//请求方式，默认是POST
		var requestType = params.method || 'POST';
		//实例化ajax对象
		var requestObj = new _options(showLoading, callback);

		//如果是本地文件不作处理
		if (requestURL.indexOf("file://") === 0) {
			if ($.isFunction(callback)) {
				callback({
					status: '000002',
					errorMsg: '本地浏览，不作数据交互处理！'
				});
			}
			return;
		}

		if (!requestData.channel) {
			//轻应用先加个统一渠道参数
			requestData.channel = 'zzckl'; //h5,要传这个渠道，图片是上传base64,并且还有登录逻辑的校验，不传channel默认是上传微信服务器,传的是pictureId

			//===add by zhuojianhai 2017-6-15添加=====
			var source = survey.getCache('source'); //入口来源，有值并且值为bj，就是北京报案历史来源
			if (source) {
				source = source.toLowerCase();
				if (source == 'bj') {
					requestData.channel = 'zzck_bj'; //北京案件列表线上拍照,要传这个渠道，没有登录逻辑的校验，图片是上传base64,不传channel默认是上传微信服务器,传的是pictureId
				}
			}
			//===add by zhuojianhai 2017-6-15添加=====
		}

		//加入列外接口

		var exception = "/wx/exploration/uploadImg";

		if (params.url == exception) {
			var requestDataEnd = requestData;
			var contentType = "application/x-www-form-urlencoded";
		} else {
			var requestDataEnd = typeof requestData == "string" ? encodeURIComponent(survey.encryptByAES(
				requestData)) : encodeURIComponent(survey.encryptByAES(JSON.stringify(requestData)));
			var contentType = "application/json; charset=utf-8";
		}

		//深度合并新的ajax请求参数
		$.extend(true, requestObj, {
			data: requestDataEnd,
			type: requestType,
			url: requestURL,
			contentType:contentType
		});

		//记录ajax发起日志
		var req = JSON.stringify(requestData);
		survey.log('***(' + requestURL + ')ajax请求数据：' + req.substring(0, 3000));
		_currentInterAddress = requestURL;
		//发起ajax请求
		$.ajax(requestObj);
	}

	survey.ajax = _ajax;
}(window.Zepto, survey);
